package com.deepcode.StringProcess.strategy.impl;


import com.deepcode.StringProcess.domain.CharObject;
import com.deepcode.StringProcess.strategy.CharacterReplacementStrategy;

/**
 * Previous Location Character Strategy:replace them with a
 *  * single character that comes before location of string.
 */
public class PreviousLocationCharacterStrategy implements CharacterReplacementStrategy {
    
    @Override
    public char replace(CharObject node, int count) {
        // 如果连续字符数量小于3，则不替换
        if (count < 3) {
            return node.getCharacter();
        }
        
        // 获取该字符前面的字符
        if (node.getPrev() != null) {
            return node.getPrev().getCharacter();
        }
        
        // 如果是第一个字符，则替换为空（删除）
        return '\0'; // 用'\0'表示删除节点
    }

    @Override
    public boolean canReplace(char ch) {
        return ch >= 'a' && ch <= 'z';
    }
}